﻿Public Class VM_UpdateCards
	Implements IDisposable

	Dim myPublisherReports As New List(Of VM_PublisherCardRecord)

	Dim db As New CongregationDataContainerLocal

	Public ReadOnly Property PublisherReports As VM_PublisherCardRecord()
		Get
			Return myPublisherReports.Where(Function(x) Not x.IsAuxPioneer AndAlso Not x.IsPioneer AndAlso Not x.IsSpecialPioneer).ToArray
		End Get
	End Property

	Public ReadOnly Property AuxPioneerReports As VM_PublisherCardRecord()
		Get
			Return myPublisherReports.Where(Function(x) x.IsAuxPioneer).ToArray
		End Get
	End Property

	Public ReadOnly Property PioneerReports As VM_PublisherCardRecord()
		Get
			Return myPublisherReports.Where(Function(x) x.IsPioneer).ToArray
		End Get
	End Property

	Public ReadOnly Property SpecialPioneerReports As VM_PublisherCardRecord()
		Get
			Return myPublisherReports.Where(Function(x) x.IsSpecialPioneer).ToArray
		End Get
	End Property

	Public ReadOnly Property IncludeHeader As Boolean
		Get
			If ReportLastSubmitted.Month = 9 Then
				'if we submitted the report in September, we need to make out new cards
				'so include the header
				Return True
			Else
				Return False
			End If
		End Get
	End Property

	Public Sub New(Optional ShowUnsubmittedTime As Boolean = False)
		If ShowUnsubmittedTime Then
			For Each publisher In db.PublisherCardDatas.OrderBy(Function(x) x.PublisherData.FullName).ToList.Where(Function(x) x.EnteredUtc > ReportLastSubmitted AndAlso Not x.IsHistorical)
				myPublisherReports.Add(New VM_PublisherCardRecord(publisher.PublisherID, publisher.Month))
			Next
		Else
			For Each publisher In db.PublisherCardDatas.OrderBy(Function(x) x.PublisherData.FullName).ToList.Where(Function(x) x.EnteredUtc < ReportLastSubmitted AndAlso x.EnteredUtc > PreviousReportLastSubmitted AndAlso Not x.IsHistorical)
				myPublisherReports.Add(New VM_PublisherCardRecord(publisher.PublisherID, publisher.Month))
			Next
		End If
	End Sub

#Region "IDisposable Support"
	Private disposedValue As Boolean ' To detect redundant calls

	' IDisposable
	Protected Overridable Sub Dispose(disposing As Boolean)
		If Not Me.disposedValue Then
			If disposing Then
				' TODO: dispose managed state (managed objects).
				db.Dispose()
			End If

			' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
			' TODO: set large fields to null.
		End If
		Me.disposedValue = True
	End Sub

	' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
	'Protected Overrides Sub Finalize()
	'    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
	'    Dispose(False)
	'    MyBase.Finalize()
	'End Sub

	' This code added by Visual Basic to correctly implement the disposable pattern.
	Public Sub Dispose() Implements IDisposable.Dispose
		' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
		Dispose(True)
		GC.SuppressFinalize(Me)
	End Sub
#End Region

End Class
